package SwingView.Assignment;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import SwingController.CmsPresenter;
import SwingView.Assignment.CmsCPAsgmComposer.AddFileActionListener;
import SwingView.Assignment.CmsCPAsgmComposer.FileEntry;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AsgmSubmissionPanel_deletelater extends JPanel{
	
	
	private JPanel jpFiles;
	private List<FileEntry> listOfFilePanel;
	private JButton jbtSubmit;
	private JLabel lblSubmission;

	public AsgmSubmissionPanel_deletelater() {
		
		listOfFilePanel = new ArrayList();
		
		setBackground(new Color(211, 211, 211));
		setOpaque(true);
		
		setPreferredSize(new Dimension(500,300));
		GridBagLayout gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);

		
		//_________________________________________________________
		//QUESTION:
		JPanel jpQ = new JPanel();
		jpQ.setOpaque(false);
		GridBagConstraints gbc_jpQ = new GridBagConstraints();
		gbc_jpQ.weightx = 1.0;
		gbc_jpQ.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpQ.gridx = 0;
		gbc_jpQ.gridy = 0;
		add(jpQ, gbc_jpQ);
		GridBagLayout gbl_jpQ = new GridBagLayout();
		jpQ.setLayout(gbl_jpQ);
		
		//Question Label
		lblSubmission = new JLabel("Submission: ");
		lblSubmission.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblQuestions = new GridBagConstraints();
		gbc_lblQuestions.insets = new Insets(10, 10, 10, 5);
		gbc_lblQuestions.weightx = 0.05;
		gbc_lblQuestions.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblQuestions.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblQuestions.gridx = 0;
		gbc_lblQuestions.gridy = 0;
		jpQ.add(lblSubmission, gbc_lblQuestions);
		
		//Add Question button
		JButton jbtAddFile = new JButton("+ Add File");
		
		jbtAddFile.setOpaque(false);
		jbtAddFile.setBorder(BorderFactory.createEmptyBorder());
		jbtAddFile.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		jbtAddFile.setForeground(Color.BLUE);
		GridBagConstraints gbc_jbtAddQuestion = new GridBagConstraints();
		gbc_jbtAddQuestion.insets = new Insets(10, 0, 0, 5);
		gbc_jbtAddQuestion.weightx = 0.5;
		gbc_jbtAddQuestion.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jbtAddQuestion.gridx = 1;
		gbc_jbtAddQuestion.gridy = 0;
		jpQ.add(jbtAddFile, gbc_jbtAddQuestion);
		
		jbtSubmit = new JButton("Submit");
		jbtSubmit.setFont(jbtSubmit.getFont().deriveFont(Font.BOLD).deriveFont(13.0f));
		jbtSubmit.setPreferredSize(new Dimension(75, 20));
		jbtSubmit.setMinimumSize(new Dimension(75,20));
		jbtSubmit.setBackground(new Color(170,170,170));
		jbtSubmit.setForeground(new Color(30,30,30));
		jbtSubmit.setOpaque(true);
		jbtSubmit.setBorder(BorderFactory.createLineBorder(new Color(30,30,30)));
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(10, 0, 0, 10);
		c.anchor = GridBagConstraints.FIRST_LINE_END;
		c.gridx = 2;
		c.gridy = 0;
		jpQ.add(jbtSubmit, c);
		
		//remove question button
		jpFiles = new JPanel();
		jpFiles.setOpaque(false);
		GridBagConstraints gbc_jpQuestions = new GridBagConstraints();
		gbc_jpQuestions.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jpQuestions.weighty = 1.0;
		gbc_jpQuestions.fill = GridBagConstraints.HORIZONTAL;
		gbc_jpQuestions.gridx = 0;
		gbc_jpQuestions.gridy = 1;
		add(jpFiles, gbc_jpQuestions);
		jpFiles.setLayout(new GridBagLayout());
		
		//ACTION LISTENERS:
		jbtAddFile.addActionListener(new AddFileActionListener());
	}
	
	private void displayListOfFile(){
		jpFiles.removeAll();
		for(int i=0; i < listOfFilePanel.size(); i++){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			jpFiles.add(listOfFilePanel.get(i),c); //add to panel for displaying
			System.out.println(i);
		}
		jpFiles.revalidate();
	}
	
	
	public void setToBeResubmission(boolean isResubmission){
		if(isResubmission){
			lblSubmission.setText("Resubmission: ");
			jbtSubmit.setText("Resubmit");
		}
		else{
			lblSubmission.setText("Submission: ");
			jbtSubmit.setText("Submit");
		}
	}

	public boolean validateFileInput(File newFile){
		//compare new file's name to all selected files
		for(FileEntry fe : listOfFilePanel){
			if(fe.getSelectedFile().getName().equals(newFile.getName())){
				String errorMsg = new String("ERROR: Your selected File, "
											+newFile.getName() 
											+ ", is already inserted on the file list");
				JOptionPane.showMessageDialog(CmsPresenter.mainView,errorMsg, "ERROR",JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
		
		return true;
	}
	
	class AddFileActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(CmsPresenter.mainView);
			if(returnVal == JFileChooser.APPROVE_OPTION){
				File selectedFile = fc.getSelectedFile();
				if(validateFileInput(selectedFile)){
					listOfFilePanel.add(new FileEntry(selectedFile));
					displayListOfFile();
				}
			}
		}
		
	}
	
	public class FileEntry extends JPanel{
		
		private static final int HEIGHT = 50;
		private static final int WIDTH = 450;
		
		private File selectedFile;
		
		private JLabel jlFileName;
		private JButton selectButton;
		private JButton removeButton;
		
		public FileEntry(File file) {
			this.selectedFile = file;
			
			setBackground(Color.WHITE);
			
			GridBagLayout gridBagLayout = new GridBagLayout();
			setLayout(gridBagLayout);
			setPreferredSize(new Dimension(WIDTH,HEIGHT));
			setMaximumSize(new Dimension(WIDTH,HEIGHT));
			setMinimumSize(new Dimension(WIDTH,HEIGHT));
			setOpaque(false);
			setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
			
			JPanel panel = new JPanel();
			panel.setOpaque(false);
			panel.setPreferredSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setMaximumSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setMinimumSize(new Dimension(WIDTH-20,HEIGHT));
			panel.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.anchor = GridBagConstraints.LINE_START;
			gbc_panel.insets = new Insets(10, 10, 5, 10);
			gbc_panel.fill = GridBagConstraints.HORIZONTAL;
			gbc_panel.gridx = 0;
			gbc_panel.gridy = 0;
			add(panel, gbc_panel);
			GridBagLayout gbl_panel = new GridBagLayout();
			panel.setLayout(gbl_panel);
			
			jlFileName = new JLabel(selectedFile.getName());
			jlFileName.setFont(new Font("Lucida Grande", Font.BOLD, 13));
			GridBagConstraints gbc_jlQuestionNum = new GridBagConstraints();
			gbc_jlQuestionNum.anchor = GridBagConstraints.LINE_START;
			gbc_jlQuestionNum.insets = new Insets(0, 0, 0, 5);
			gbc_jlQuestionNum.fill = GridBagConstraints.HORIZONTAL;
			gbc_jlQuestionNum.gridx = 0;
			gbc_jlQuestionNum.gridy = 0;
			gbc_jlQuestionNum.gridwidth = 2;
			panel.add(jlFileName, gbc_jlQuestionNum);
			
			selectButton = new JButton("Reselect File");
			selectButton.setForeground(Color.BLUE);
			selectButton.setOpaque(false);
			selectButton.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_jbtClear = new GridBagConstraints();
			gbc_jbtClear.anchor = GridBagConstraints.LINE_END;
			gbc_jbtClear.weightx = 1.0;
			gbc_jbtClear.insets = new Insets(0, 0, 0, 30);
			gbc_jbtClear.gridx = 0;
			gbc_jbtClear.gridy = 1;
			panel.add(selectButton, gbc_jbtClear);
			
			removeButton = new JButton("Remove");
			removeButton.setForeground(Color.BLUE);
			removeButton.setOpaque(false);
			removeButton.setBorder(BorderFactory.createEmptyBorder());
			GridBagConstraints gbc_btnRemove = new GridBagConstraints();
			gbc_btnRemove.anchor = GridBagConstraints.LINE_END;
			gbc_btnRemove.gridx = 1;
			gbc_btnRemove.gridy = 1;
			panel.add(removeButton, gbc_btnRemove);
			
			
			//ACTION LISTENER:
			selectButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(CmsPresenter.mainView);
					if(returnVal == JFileChooser.APPROVE_OPTION){
						
						if(validateFileInput(fc.getSelectedFile())){
							selectedFile = new File(fc.getSelectedFile().getPath());
							jlFileName.setText(selectedFile.getName());
						}
					}
				}
				
			});
			
			removeButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					listOfFilePanel.remove(FileEntry.this);
					displayListOfFile();
				}
				
			});
		}
		
		public File getSelectedFile(){
			return this.selectedFile;
		}
	}
	
	
	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		g.fillRoundRect(8, 0, this.getWidth() - 16, this.getHeight() - 8, 10, 10);
	}	
}
